package com.ayu.commentbackend.controller;

import com.ayu.commentbackend.StatusConstant;
import com.ayu.commentbackend.pojo.dto.BlogAddDTO;
import com.ayu.commentbackend.pojo.dto.BlogPageQueryDTO;
import com.ayu.commentbackend.pojo.dto.BlogUpdateDTO;
import com.ayu.commentbackend.pojo.result.PageResult;
import com.ayu.commentbackend.pojo.result.Result;
import com.ayu.commentbackend.service.AIService;
import com.ayu.commentbackend.service.BlogService;
import com.ayu.commentbackend.utils.DeepSeekUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@Api(tags = "帖子相关")
@RequestMapping("/blog")
@Slf4j
public class BlogController {
    @Autowired
    BlogService blogService;
    @Autowired
    AIService aiService;

    @PostMapping("/pageQuery")
    @ApiOperation("带关键字的分页查询")
    public Result<PageResult> pageQuery(@RequestBody BlogPageQueryDTO blogPageQueryDTO) {
        return Result.success(blogService.pageQuery(blogPageQueryDTO));
    }

    @PostMapping
    @ApiOperation("新增帖子")
    public Result add(@RequestBody BlogAddDTO blogAddDTO) {
        Long id = blogService.add(blogAddDTO);

        if (blogAddDTO.getStatus() == StatusConstant.READY_FOR_AI) {
            blogService.updateAiStatus(StatusConstant.AI_PROCESSING, id);

            // 安全拼接内容
            String content = Stream.of(blogAddDTO.getGoodEvent(),
                            blogAddDTO.getBadEvent(),
                            blogAddDTO.getOtherEvent())
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining());

            aiService.deepSeek(content, id); // 通过Service调用

            return Result.success("正在审核中...");
        }

        return Result.success();
    }

    @PutMapping
    @ApiOperation("更新帖子")
    public Result update(@RequestBody BlogUpdateDTO blogUpdateDTO) {
        blogService.update(blogUpdateDTO);

        if (blogUpdateDTO.getStatus() == StatusConstant.READY_FOR_AI) {
            blogService.updateAiStatus(StatusConstant.AI_PROCESSING, blogUpdateDTO.getId());

            // 安全拼接内容
            String content = Stream.of(blogUpdateDTO.getGoodEvent(),
                            blogUpdateDTO.getBadEvent(),
                            blogUpdateDTO.getOtherEvent())
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining());

            aiService.deepSeek(content, blogUpdateDTO.getId());

            return Result.success("正在审核中...");
        }

        return Result.success();
    }

    @GetMapping("/detail")
    @ApiOperation("根据帖子Id获取帖子详情")
    public Result getDetailById(@RequestParam("id") Integer id) {
        return Result.success(blogService.detail(id));
    }

    @GetMapping("/list")
    @ApiOperation("获取某用户创建的帖子")
    public Result getByUserId(@RequestParam("createUser") Integer createUser) {
        return Result.success(blogService.getByUserId(createUser));
    }

    @DeleteMapping
    @ApiOperation("根据帖子id删除帖子")
    public Result delete(@RequestParam("id") Integer id) {
        blogService.delete(id);
        return Result.success("删除成功");
    }
}
